package com.wusuowei.config.exception;


import com.wusuowei.common.utils.R;
import com.wusuowei.common.utils.RespEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = AccessDeniedException.class)
    public R handler(AccessDeniedException e){
        log.error("认证异常：---------{}"+e.getMessage());
        return R.error(RespEnum.UNAUTHORIZEDEDERROR);
    }

    @ExceptionHandler(value = RuntimeException.class)
    public R handler(RuntimeException e){
        log.error("运行时异常：---------{}"+e.getMessage());
        return R.error(e.getMessage());
    }

    /**
     * 如果抛出的的是ServiceException，则调用该方法
     * @param se 业务异常
     * @return Result
     */
    @ExceptionHandler(ServiceException.class)
    public R handle(ServiceException se){
        return R.error(se.getCode(), se.getMessage());
    }

}
